package com.nus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleNews;
import com.nus.representations.RemindDetails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SportsActivity extends Activity {
	// private ListView lv;
	public static RemindDetails rd;

	public CharSequence cs1;
	public CharSequence cs2;
	public CharSequence cs3;
	public ImageButton home;
	public CharSequence cs4;
	protected TextView title;
	// private String lv_arr[];
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.sportslayout);
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		 title = (TextView) findViewById(R.id.titleheader);
		 title.setText("Sports Booking");
		 ImageView b = (ImageView)findViewById(R.id.home);
		  b.setImageDrawable(getResources().getDrawable(R.drawable.sports));
		final DatePicker dp = (DatePicker) findViewById(R.id.datepicker);
		/*
		 * home = (ImageButton) findViewById(R.id.home);
		 * home.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub startActivity(new Intent("com.nus.OptionsActivity")); } });
		 */
		boolean netconn = checkInternetConnection();
		if (!netconn) {
			Toast.makeText(getApplicationContext(),
					"Please check your internet or 3G connection",
					Toast.LENGTH_LONG).show();
		}
		Spinner spinner_fac = (Spinner) findViewById(R.id.spin_fac);
		final ArrayAdapter<CharSequence> adapter_fac = ArrayAdapter
				.createFromResource(this, R.array.Select_Game,
						android.R.layout.simple_spinner_item);
		adapter_fac
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_fac.setAdapter(adapter_fac);

		spinner_fac
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						cs1 = adapter_fac.getItem(arg2);
						Log.i("game", cs1.toString());
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});

		Spinner spin_noofguest = (Spinner) findViewById(R.id.spin_noofguest);
		final ArrayAdapter<CharSequence> adapter_spin_noofguest = ArrayAdapter
				.createFromResource(this, R.array.spin_noofguest,
						android.R.layout.simple_spinner_item);
		adapter_spin_noofguest
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_noofguest.setAdapter(adapter_spin_noofguest);

		spin_noofguest
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						cs2 = adapter_spin_noofguest.getItem(arg2);
						Log.i("no of guest", cs2.toString());
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});

		Spinner select_time = (Spinner) findViewById(R.id.select_time);
		final ArrayAdapter<CharSequence> adapter_select_time = ArrayAdapter
				.createFromResource(this, R.array.select_time,
						android.R.layout.simple_spinner_item);
		adapter_select_time
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		select_time.setAdapter(adapter_select_time);

		select_time
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						// Object item = arg0.getItemAtPosition(arg2);
						cs3 = adapter_select_time.getItem(arg2);
						Log.i("select time", cs3.toString());
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});

		Spinner select_court = (Spinner) findViewById(R.id.select_court);
		final ArrayAdapter<CharSequence> adapter_select_court = ArrayAdapter
				.createFromResource(this, R.array.select_court,
						android.R.layout.simple_spinner_item);
		adapter_select_court
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		select_court.setAdapter(adapter_select_court);

		select_court
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						cs4 = adapter_select_court.getItem(arg2);
						Log.i("select court", cs4.toString());
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});

		final Button book = (Button) findViewById(R.id.but_cont);
		book.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				StringBuffer desc = new StringBuffer();
				desc.append("Play ");
				desc.append(cs1);
				desc.append(":");
				desc.append(cs3);
				desc.append(":");
				desc.append(cs4);
				rd = new RemindDetails(dp.getDayOfMonth(), dp.getMonth(), dp
						.getYear(), cs3.toString(), desc.toString());

				startActivity(new Intent("com.nus.SportsFinalActivity"));
			}
		});

	}

	public String getResponseText(String stringUrl) throws IOException {
		StringBuilder response = new StringBuilder();

		URL url = new URL(stringUrl);
		HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
		if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					httpconn.getInputStream()), 8192);
			String strLine = null;
			while ((strLine = input.readLine()) != null) {
				response.append(strLine);
			}
			input.close();
		}
		return response.toString();
	}

	public boolean checkInternetConnection() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;

		} else {
			return false;
		}
	}
}
